#include "exti1.h"
#include "usart.h"
#include <string.h>

u8 flag ;
extern u8 data_buf[4];
void MyExti1_init(void)
{
	EXTI_InitTypeDef EXTI_InitStructure;		//EXTIx中断线配置
	NVIC_InitTypeDef NVIC_InitStructure;		//EXTIx中断向量配置
	GPIO_InitTypeDef GPIO_InitStructure;
	

	//配置按键引脚为下降沿中断	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);	
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB,EXTI_PinSource1);	//将EXTI13指向PC13
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	EXTI_InitStructure.EXTI_Line=EXTI_Line1;
	EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
	EXTI_InitStructure.EXTI_LineCmd=ENABLE;
	EXTI_Init(&EXTI_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel=EXTI0_1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPriority=1;
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Init(&NVIC_InitStructure);

}
uint8_t temp[3];
uint8_t x_tp;
void EXTI0_1_IRQHandler(void)
{
	uint8_t i,j;
	if (RESET != EXTI_GetITStatus(EXTI_Line1)&&flag == 0) 
    {
		Delay_ms(50);
		//清除中断标记
        SI12TCH_IIC_EN //开iic
		SI12_ReadData();
		SI12TCH_IIC_OFF //关iic
		x_tp = 0 ;
		for(i=0;i<3;i++)
		{
			for(j=0;j<4;j++)
			{
				if(((data_buf[i]>>(2*j))&0x03)!= 0)
				{
					x_tp++;
					flag = (i*4+j)+1;
//					printf("&&%d\r\n",flag);
				}
			}
		}
		
		//两个以上按键，清除flag ，保存数据以便松开按键但没有全部松开时比对
		if(x_tp>1)
		{
//			printf("xx%02x,%02x,%02x\r\n",data_buf[0],data_buf[1],data_buf[2]);
			memcpy(temp,data_buf,3);
			flag = 0;
//			printf("$$%d\r\n",flag);
		}
		
		//按键松开，清空按下的缓冲数据
		else if(x_tp == 0)
		{
			memset(temp ,0,3);
//			printf("&&%d\r\n",flag);
		}
		
		/*
		只有一个按键按下，判断此次是否为按键松下时的中断
		若是flag清零
		不是flag正常输出
		*/
		else
		{
			if(temp[0] != 0)
			{
				for(i=0;i<3;i++)
				{
					for(j=0;j<4;j++)
					{
						if(((temp[i]>>(2*j))&0x03)!= 0)
						{
							if(flag == i*4+j+1)
							{
								flag = 0;
							}
						}
					}
				}
			}
		}
		
		
//		if( (data_buf[0] & 0x03) != 0 )      flag = 1; 
//		if( ((data_buf[0]>>2) & 0x03) != 0 ) flag = 2;  
//		if( ((data_buf[0]>>4) & 0x03) != 0 ) flag = 3;  
//		if( ((data_buf[0]>>6) & 0x03) != 0 ) flag = 4;  
//		 
//		if( (data_buf[1] & 0x03) != 0 )      flag = 5;  
//		if( ((data_buf[1]>>2) & 0x03) != 0 ) flag = 6;  
//		if( ((data_buf[1]>>4) & 0x03) != 0 ) flag = 7; 
//		if( ((data_buf[1]>>6) & 0x03) != 0 ) flag = 8;  
//		 
//		if( (data_buf[2] & 0x03) != 0 )      flag = 9;  
//		if( ((data_buf[2]>>2) & 0x03) != 0 ) flag = 10; 
//		if( ((data_buf[2]>>4) & 0x03) != 0 ) flag = 11; 
//		if( ((data_buf[2]>>6) & 0x03) != 0 ) flag = 12;
		
//		if( ((data_buf[3]) & 0x03) != 0 ) flag = 13; 
//		if( ((data_buf[3]>>2) & 0x03) != 0 ) flag = 14;
    }
	EXTI_ClearITPendingBit(EXTI_Line1);
	Delay_ms(25);
}

